package cn.restcloud.example.controller.rdb;

import cn.restcloud.framework.core.annotation.bean.BeanConfig;
import cn.restcloud.framework.core.annotation.bean.BeanType;
import cn.restcloud.framework.core.annotation.rest.BodyDataType;
import cn.restcloud.framework.core.annotation.rest.MethodType;
import cn.restcloud.framework.core.annotation.rest.Param;
import cn.restcloud.framework.core.annotation.rest.Params;
import cn.restcloud.framework.core.annotation.rest.RestConfig;
import cn.restcloud.framework.core.annotation.rest.RestModelId;
import cn.restcloud.framework.core.annotation.rest.RestRequestBody;
import java.util.List;
import org.bson.Document;
import cn.restcloud.framework.core.base.IBasePageBean;
import cn.restcloud.framework.core.context.AppContext;
import cn.restcloud.framework.core.util.RequestUtil;
import cn.restcloud.framework.core.util.db.rdb.RdbDataModelUtil;
import cn.restcloud.framework.view.base.DocAndView;
import cn.restcloud.framework.view.util.ViewUtil;

/**
 * 通过使用RdbDataModelUtil类快速操作数据模型发布服务
 * 只需要在Web界面中创建数据模型或业务模型，无需建立dao,daoimpl,modelbean类
 * @author Administrator
 *
 */

@BeanConfig(appId = "example",beanId = "RdbDataModelUtilDemoRest",beanName = "DataModelUtil类使用示例",beanType = BeanType.CONTROLLER,autowired = false,singleton = true,hotMode = true)
@RestConfig(url = "/rest/example/rdb/common")
@RestModelId("example.UserModel")
public class RdbDataModelUtilDemoRest {
	private String modelId="example.UserModel"; //这里指定要操作的实体模型或业务模型的唯一id
	
	@RestConfig(configName = "列出数据模型的所有记录",method = MethodType.GET,url = "/listAll")
	public DocAndView listAll()throws Exception{
		List<Document> docs=RdbDataModelUtil.listAll(modelId);
		return ViewUtil.getJsonView(docs);
	}
	
	@RestConfig(configName = "分页列出数据模型的记录",method = MethodType.GET,url = "/listByPage")
	@Params({
		@Param(id = "PageNo", name = "当前页兼容page",defaultValue="1"),
		@Param(id = "pageSize", name = "每页显示数兼容rows",defaultValue="20"),
		@Param(id = "orderBy", name = "动态排序字段"),
		@Param(id = "sortDirection", name = "排序方向",sampleValue="ASC|DESC")
	})
	public DocAndView listByPage(int pageNo,int pageSize,String orderBy,String sortDirection )throws Exception{
		String sqlWhere="userId=? age=?"; //指定数据选择条件，没有可以为空值
		IBasePageBean<Document> pageBean=RdbDataModelUtil.listByPage(modelId, sqlWhere, orderBy, sortDirection, pageNo, pageSize, "admin",23);
		return ViewUtil.getJsonView(pageBean);
	}
	

	@RestConfig(configName = "根据sql条件返回多条数据记录",method = MethodType.GET,url = "/listByFilters")
	public DocAndView listByFilters()throws Exception{
		String sqlWhere="";//不带where关键字的任意sql条件格式：userId='admin' or userId='test'
		List<Document> listDocs=RdbDataModelUtil.listBySqlWhere(modelId,sqlWhere);
		return ViewUtil.getJsonView(listDocs);
	}
	
	@RestConfig(configName = "根据sql条件返回一条数据记录",method = MethodType.GET,url = "/getByFilters")
	public Document getByFilters()throws Exception{
		String sqlWhere="";//不带where关键字的任意sql条件格式：userId='admin' or userId='test'
		Document doc=RdbDataModelUtil.getBySqlWhere(modelId,sqlWhere);
		return doc;
	}
	
	@RestConfig(configName = "根据关键字段的值查询数据记录",method = MethodType.GET,url = "/getById")
	@Params({
		@Param(id = "keyValue", name = "关键字段的值",required=true)
	})
	public Document getById(String keyValue)throws Exception{
		Document doc=RdbDataModelUtil.getByKeyValue(modelId, keyValue);
		return doc;
	}
	
	@RestConfig(configName = "把POST键值对参数保存或更新到数据模型中",method = MethodType.POST,url = "/save")
	@Params({
		@Param(id = "userId", name = "用户id",required=true),
	})
	public DocAndView saveRequestBody()throws Exception{
		//把所有请求数据对应存入到数据模型中，系统根据id唯一字段进行判断是更新还是插入
		int i=RdbDataModelUtil.save(modelId,RequestUtil.getContextDocument());
		return ViewUtil.getSuccessView(String.valueOf(i));
	}
	
	@RestConfig(configName = "把JSON数组保存或更新到数据模型中",method = MethodType.POST,url = "/saveBodyArray")
	@RestRequestBody(type=BodyDataType.JSONARRY)
	@Params({
		@Param(id = "userId", name = "用户id",required=true),
	})
	public DocAndView saveRequestBodyArray(List<Document> docs)throws Exception{
		//把所有请求数据对应存入到数据模型中，系统根据id唯一字段进行判断是更新还是插入
		int i=RdbDataModelUtil.save(modelId,docs);
		return ViewUtil.getSuccessView(String.valueOf(i));
	}
	
	@RestConfig(configName = "根据关键字段的值删除数据模型中对应的多条记录",method = MethodType.POST,url = "/delete")
	@Params({
		@Param(id = "keyValues", name = "唯一关键字段的值,多个用逗号分隔",required=true)
	})
	public DocAndView deleteByIds(String keyValues)throws Exception{
		int i=RdbDataModelUtil.deleteByKeyValue(modelId, keyValues);
		DocAndView dv=ViewUtil.getSuccessView(String.valueOf(i));
		return dv;
	}
	
	@RestConfig(configName = "根据过滤条件删除数据模型中对应的多条记录",method = MethodType.POST,url = "/deleteByFilters")
	public DocAndView deleteByFilters()throws Exception{
		String sqlWhere="userId=?"; //不带where关键字的任意条件格式 :userId='admin' and userId='test'
		int i=RdbDataModelUtil.deleteBySqlWhere(modelId, sqlWhere,AppContext.getUserId());
		DocAndView dv=ViewUtil.getSuccessView(String.valueOf(i));
		return dv;
	}
	
}
